import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/internal/operators';
import { API_CONFIG, ServicesModule } from './services.module';
import { Banner, HotTag, SongSheet } from './data-types/common.type';

@Injectable({
  providedIn: ServicesModule
})
export class HomeService {

  constructor(private http: HttpClient, @Inject(API_CONFIG) private url: string) { }
  getBanner(): Observable<Banner[]> {
    return this.http.get(this.url + '/banner')
      .pipe(map((res: { banners: Banner[]}) => res.banners));
  }
  getHotTags(): Observable<HotTag[]> {
    return this.http.get(this.url + '/playlist/hot')
      .pipe(map((res: { tags: HotTag[]}) => {
        return res.tags.sort((x: HotTag, y: HotTag) => x.position - y.position).slice(0, 5);
      }));
  }
  getPersonalSheetList(): Observable<SongSheet[]> {
    return this.http.get(this.url + '/personalized')
      .pipe(map((res: { result: SongSheet[]}) => {
        return res.result.slice(0, 16);
      }));
  }
}
